<template>
  <div class="app-container">
    <template>
      <el-row>
        <el-col :span="24">
          <div class="h_top">
            <div class="search_box">
              <el-form :model="query" :inline="true">
                <el-row :gutter="10">
                  <el-col :xl="4" :lg="4" :md="5" :sm="6">
                    <el-form-item>
                      <div class="search_lable">编 号</div>
                      <el-input v-model="query.coding" size="mini" />
                    </el-form-item>
                  </el-col>
                  <el-col :xl="4" :lg="4" :md="5" :sm="6">
                    <el-form-item>
                      <div class="search_lable">名 称</div>
                      <el-input v-model="query.name" size="mini" />
                    </el-form-item>
                  </el-col>
                  <el-col :xl="4" :lg="3" :md="5" :sm="6">
                    <el-form-item>
                      <div class="search_lable">状 态</div>
                      <el-select v-model="query.status" size="mini">
                        <el-option v-for="(item, index) in contractStatus" :key="index" :label="item" :value="index" />
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col :xl="4" :lg="5" :md="6" :sm="6">
                    <el-form-item>
                      <div class="search_lable">供应商</div>
                      <v-select v-model="query.provider_id" action="providerList" />
                    </el-form-item>
                  </el-col>
                  <el-col :xl="3" :lg="3" :md="5" :sm="6">
                    <el-form-item>
                      <div class="search_lable">实体位置</div>
                      <v-select v-model="query.position_id" action="positionList" />
                    </el-form-item>
                  </el-col>
                  <el-col :xl="3" :lg="3" :md="5" :sm="6">
                    <el-form-item>
                      <div class="search_lable">负责人</div>
                      <v-select v-model="query.admin_id" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="2">
                    <el-form-item>
                      <div class="search_lable">操 作</div>
                      <el-button type="primary" icon="el-icon-search" size="mini" @click="search">查找</el-button>
                    </el-form-item>
                  </el-col>
                </el-row>
              </el-form>
            </div>
            <div class="toolBars">
              <div class="buttonBox">
                <el-dropdown :hide-on-click="false">
                  <el-button type="primary" icon="el-icon-tickets" size="mini" @click="exportHandler">导出<i class="el-icon-arrow-down el-icon--right"></i></el-button>
                  <el-dropdown-menu slot="dropdown">
                    <el-checkbox-group v-model="needExport">
                      <el-dropdown-item><el-checkbox label="coding">编号</el-checkbox></el-dropdown-item>
                      <el-dropdown-item><el-checkbox label="name">名称</el-checkbox></el-dropdown-item>
                      <el-dropdown-item><el-checkbox label="providers.coding">供应商</el-checkbox></el-dropdown-item>
                      <el-dropdown-item><el-checkbox label="start_date">开始日期</el-checkbox></el-dropdown-item>
                      <el-dropdown-item><el-checkbox label="end_date">结束日期</el-checkbox></el-dropdown-item>
                      <el-dropdown-item><el-checkbox label="position.name">实体位置</el-checkbox></el-dropdown-item>
                      <el-dropdown-item><el-checkbox label="status">状态</el-checkbox></el-dropdown-item>
                      <el-dropdown-item><el-checkbox label="admin.name">负责人</el-checkbox></el-dropdown-item>
                      <el-dropdown-item><el-checkbox label="admin.phone">电话</el-checkbox></el-dropdown-item>
                    </el-checkbox-group>
                  </el-dropdown-menu>
                </el-dropdown>
              </div>
              <div class="buttonBox">
                <el-button type="primary" icon="el-icon-document-add" size="mini" @click="addHandler">增加</el-button>
              </div>
              <div class="buttonBox">
                <el-button type="primary" icon="el-icon-delete" size="mini" :disabled="!selection.length" @click="remove">删除</el-button>
              </div>
              <div class="buttonBox">
                <el-button type="primary" icon="el-icon-refresh" size="mini" @click="reloadHandler">刷新</el-button>
              </div>
              <div class="fr">总数：<span>{{total}}</span></div>
            </div>
          </div>
        </el-col>
      </el-row>
    </template>
    <div>
      <el-table v-loading="loading" :data="contractList" size="small" stripe border :header-cell-style="{background:'#eef1f6',color:'#606266'}" style="width: 100%" @selection-change="selectionHandler">
        <el-table-column type="index" width="50" label="序号" align="center" />
        <el-table-column type="selection" width="45" align="center" />
        <el-table-column prop="coding" label="编号" width="100" align="center" />
        <el-table-column prop="name" label="名称" />
        <el-table-column prop="status" label="状态" width="100" align="center" :formatter="statusFormat" />
        <el-table-column prop="providers.name" label="合同商" width="200" align="center" />
        <el-table-column prop="start_date" label="开始时间" width="100" align="center" />
        <el-table-column prop="end_date" label="结束时间" width="200" align="center" />
        <el-table-column fixed="right" label="操作" width="200" align="center">
          <template scope="scope">
            <el-button type="primary" size="mini" @click="updateHandler(scope.row.id)">编辑</el-button>
            <el-button size="mini" type="danger" @click="confirmDel(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination class="page" background :page-size="query.limit" layout="total,prev,pager,next,jumper" :total="total" :current-page.sync="query.page" @current-change="index" @size-change="index" />
    </div>
  </div>
</template>
<script>
import API from '@/api/procurement/contract'
import { contractStatus } from '@/utils/options'
import VSelect from '@/components/select'
import xlsx from 'xlsx'
import exportMethod from '@/mixins/export'
export default {
  inject: ['reload', 'goUrl', 'delOne', 'delMany', 'getStatusFormat'],
  components: {
    VSelect
  },
  mixins: [exportMethod],
  data() {
    return {
      needExport: [],
      loading: false,
      total: 0,
      query: { page: 1, limit: 15 },
      contractList: [],
      selection: [],
      contractStatus
    }
  },
  watch: {},
  created() {
    this.index()
  },
  methods: {
    search() {
      this.query.page = 1
      this.index()
    },
    selectionHandler(selection) {
      this.selection = selection
    },
    index() {
      this.loading = true
      API.contractList(this.query).then(response => {
        this.total = response.data.total
        this.contractList = response.data.data
      }).finally(() => {
        this.loading = false
      })
    },
    exportHandler() {
      const data = []
      const titleObj = {
        'coding': '编号',
        'name': '名称',
        'providers.coding': '供应商',
        'start_date': '开始日期',
        'end_date': '结束日期',
        'position.name': '实体位置',
        'status': '状态',
        'admin.name': '负责人',
        'admin.phone': '电话'
      }
      if (this.needExport.length) {
        const title = []
        for (const key of this.needExport) {
          title.push(titleObj[key])
        }
        data.push(title)
      } else {
        data.push(Object.values(titleObj))
      }
      if (this.selection.length === 0) {
        this.$message.error('请选择要到导出的数据')
        return false
      } else {
        this.selection.forEach((item) => {
          if (item.status === 0) {
            item.statusOut = '在期'
          } else if (item.status === 1) {
            item.statusOut = '过期'
          } else if (item.status === 2) {
            item.statusOut = '作废'
          }
          if (this.needExport.length) {
            const d = []
            for (const key of this.needExport) {
              if (key.includes('.')) {
                let value = item
                const tag = key.split('.')
                for (const k of tag) {
                  value = value[k]
                }
                d.push(value)
              } else {
                d.push(item[key])
              }
            }
            data.push(d)
          } else {
            data.push([
              item.coding,
              item.name,
              item.providers.coding,
              item.start_date,
              item.end_date,
              item.position.name,
              item.statusOut,
              item.admin.name,
              item.admin.phone
            ])
          }
        })
      }
      const workbook = {
        SheetNames: ['合同数据'],
        Sheets: {
          '合同数据': xlsx.utils.aoa_to_sheet(data)
        }
      }
      this.exportExec(workbook, '合同数据报表.xlsx')
    },
    remove() {
      const selection = this.selection
      this.delMany(API, selection)
    },
    confirmDel(id) {
      this.delOne(API, id, false)
    },
    addHandler() {
      this.goUrl('/procurement/contract/contract_add')
    },
    updateHandler(id) {
      const url = `/procurement/contract/contract_edit/${id}`
      this.goUrl(url)
    },
    reloadHandler() {
      this.reload()
    },
    statusFormat(row) {
      return this.getStatusFormat(row)
    }
  }
}
</script>
